//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.eResearch.Common.Reflection
{
    using System;
    using System.Configuration;

    /// <summary>
    /// Contains a number of helper methods for working with reflection and creating objects.
    /// </summary>
    public static class ReflectionHelper
    {
        /// <summary>
        /// Creates a new instance of the type specified in the appSettings section of the App.Config file.
        /// The key is configName and the value is a fully-qualified type name
        /// (e.g. "Name.Space.ClassName, Assembly.Name.WithoutDllExtension").
        /// </summary>
        /// <typeparam name="T">The type of the object to create.</typeparam>
        /// <param name="configName">Name of the key in the appSettings file.</param>
        /// <returns>A new instance of the specified type.</returns>
        public static T CreateInstance<T>(string configName)
        {
            string typeName = ConfigurationManager.AppSettings[configName];
            return (T)Activator.CreateInstance(System.Type.GetType(typeName));
        }
    }
}
